package com.d3works.apps.account.web;

import com.d3works.apps.account.db.FixedAssetLine;
import com.d3works.apps.account.db.repo.FixedAssetLineRepository;
import com.d3works.apps.account.exception.AccountExceptionMessage;
import com.d3works.apps.account.service.fixedasset.FixedAssetLineMoveService;
import com.d3works.exception.service.TraceBackService;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Singleton;

@Singleton
public class FixedAssetLineController {

  public void realize(ActionRequest request, ActionResponse response) {
    FixedAssetLine fixedAssetLine = request.getContext().asType(FixedAssetLine.class);
    fixedAssetLine = Beans.get(FixedAssetLineRepository.class).find(fixedAssetLine.getId());

    try {
      Beans.get(FixedAssetLineMoveService.class).realize(fixedAssetLine, false, true);
      response.setReload(true);
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }

  public void simulate(ActionRequest request, ActionResponse response) {
    FixedAssetLine fixedAssetLine = request.getContext().asType(FixedAssetLine.class);
    fixedAssetLine = Beans.get(FixedAssetLineRepository.class).find(fixedAssetLine.getId());

    try {
      if (Beans.get(FixedAssetLineMoveService.class).canSimulate(fixedAssetLine)) {
        Beans.get(FixedAssetLineMoveService.class).simulate(fixedAssetLine);
      } else {
        response.setError(I18n.get(AccountExceptionMessage.IMMO_FIXED_ASSET_CAN_NOT_SIMULATE));
      }
      response.setReload(true);
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }
}
